﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SYS.Common
{
    public class RegexModel
    {
        private string pattern;
        /// <summary>
        /// 节点正则
        /// </summary>
        public string Pattern
        {
            get { return pattern; }
            set { pattern = value; }
        }
        private string originText;
        /// <summary>
        /// 原始文本
        /// </summary>
        public string OriginText
        {
            get { return originText; }
            set { originText = value.Replace("\n",""); }
        }
        private string matchText;

        public string MatchText
        {
            get { return this.GetText(); }
            set { matchText = value; }
        }
        public string GetText(string originText, string pattern) {
            this.OriginText = originText;
            this.Pattern = pattern;
            return this.GetText();
        }
        public string GetText() {
            if (string.IsNullOrWhiteSpace(this.OriginText))
            {
                throw new Exception("原始文本为空");
            }
            if (string.IsNullOrWhiteSpace(this.Pattern))
            {
                return this.OriginText;
            }
            Regex r = new Regex(this.Pattern);
            MatchCollection mc= r.Matches(this.OriginText);
            if (mc.Count>0)
            {
                string str="";
                for (int i = 0; i < mc.Count; i++)
                {
                    str+= mc[i].Value+" ";
                }
                return str;
            }
            else
            {
                return null;
            }
        }
        private List<RegexModel> sonModel;
        /// <summary>
        /// 子节点
        /// </summary>
        public List<RegexModel> SonModel
        {
            get { return sonModel; }
            set { sonModel = value; }
        }
        private string sonPattern;
        /// <summary>
        /// 子节点正则
        /// </summary>
        public string SonPattern
        {
            get { return sonPattern; }
            set { sonPattern = value; }
        }
        private Dictionary<string, string> itemPattern;
        /// <summary>
        /// 当前匹配中获取指定匹配数据
        /// </summary>
        public Dictionary<string, string> ItemPattern
        {
            get { return itemPattern; }
            set { itemPattern = value; }
        }
        public List<Dictionary<string, string[]>> GetItems()
        {
            MatchCollection mc;
            if (this.ItemPattern.Keys.Count == 0)
            {
                throw new Exception("未设置要查找的项目");
            }
            List<Dictionary<string, string[]>> d = new List<Dictionary<string, string[]>>();
            var t = this.OriginText;
            Common.Log.txtLog(t);
            Dictionary<string, string[]> result = new Dictionary<string, string[]>();
            foreach (var item in this.ItemPattern.Keys)
            {
                Regex r = new Regex(ItemPattern[item]);
                mc = r.Matches(t);
                if (mc.Count > 0)
                {
                    string[] tempResult = new string[mc.Count];
                    for (int j = 0; j < mc.Count; j++)
                    {
                        tempResult[j] = mc[j].Groups[item].Value;
                    }
                    result.Add(item, tempResult);
                }
            }
            d.Add(result);
            return d;
        }
        /// <summary>
        /// 获取所有项目
        /// </summary>
        /// <returns></returns>
        public List<Dictionary<string, string[]>> GetAllItems() {
            List<Dictionary<string, string[]>> results=new List<Dictionary<string,string[]>>();
                //this.GetItems();
            List<RegexModel> sons = this.GetSonModel();
            for (int i = 0; i < sons.Count; i++)
            {
                var son = sons[i];
                results.AddRange(son.GetItems());
            }
            return results;
        }

        public List<RegexModel> GetSonModel()
        {
            if (string.IsNullOrWhiteSpace(this.SonPattern))
            {
                throw new Exception("子正则为空");
            }
            List<RegexModel> son = new List<RegexModel>();
            string t = this.MatchText;
            MatchCollection mc;
            Regex r = new Regex(this.SonPattern);
            mc = r.Matches(t);
            if (mc.Count > 0)
            {
                for (int j = 0; j < mc.Count; j++)
                {
                    RegexModel rm = new RegexModel();
                    rm.OriginText = mc[j].Value;
                    rm.ItemPattern = this.ItemPattern;
                    son.Add(rm);
                }
            }
            return son;
        }
    }
}
